<template>
	<div>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="type.filters">
				<el-form-item>
					<el-input v-model="type.filters.keywords" placeholder="类型名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="keywordsQuery1">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd1">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="type.dirTypes" highlight-current-row v-loading="type.listLoading" @selection-change="selsChange1" style="width: 100%;" @row-dblclick="openDrawer">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column prop="name" label="类型名称" width="400px">
			</el-table-column>
      <el-table-column prop="sn" label="类型编号" width="400px">
      </el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit1(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel1(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove1" :disabled="this.type.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange1" :current-page="type.currentPage" :page-size="type.pageSize" :total="type.totals" style="float:right;">
			</el-pagination>
		</el-col>

    <!-- 添加或修改的对话框
    el-dialog - elementUi提供的对话框 = 模态框
      :title="title" - 动态改变标题
      :visible.sync="saveFormVisible"  - 模态框的显示状态
      :close-on-click-modal="false" - 关闭模态框只能通过模态框的x关闭，点击空白处不能关闭模态框
    el-form - 表单
      :model="saveForm" - 绑定表单对象
      label-width="80px" - 标签宽度
      ref="saveForm" - 为当前模态框取名
      prop="name" - 如果当前字段要验证，就必须写，出现*
      auto-complete="off" - 不自动提示
-->
    <el-dialog :title="title" :visible.sync="type.saveFormVisible" :close-on-click-modal="false">
      <el-form :model="type.saveForm" label-width="80px" ref="saveForm1">
        <el-form-item label="数据类型" prop="name">
          <el-input v-model="type.saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="数据编号" prop="sn">
          <el-input v-model="type.saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="type.saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit1">提交	</el-button>
      </div>
    </el-dialog>

	</div>
</template>

<script>
	export default {
		data() {
			return {
        title: '',
        type: {
          filters: {
            keywords: ''
          },
          dirTypes: [],
          totals: 0,
          currentPage: 1,
          pageSize: 5,
          listLoading: false,
          sels: [],//列表选中列
          saveFormVisible: false,//编辑界面是否显示
          // 添加或界面数据
          saveForm: {
            id: null,
            name: '',
            sn: ''
          }
        }

			}
		},
		methods: {
      keywordsQuery1(){
        this.type.currentPage = 1;
        this.getDirTypes();
      },
			handleCurrentChange1(val) {
				this.type.currentPage = val;
				this.getDirTypes();
			},
			//获取用户列表
			getDirTypes() {
				let para = {
					currentPage: this.type.currentPage,
          pageSize: this.type.pageSize,
					keywords: this.type.filters.keywords
				};
				this.type.listLoading = true;
        this.$http.post("/dictionaryType",para).then(result=>{
          this.type.totals = result.data.totals;
          this.type.dirTypes = result.data.data;
          this.type.listLoading = false;
        }).catch(result=>{
          this.$message.error('请求错误');
        })
			},
			//删除
			handleDel1: function (index, row) {
				this.$confirm('确认删除该记录吗?', '温馨提示', {
					type: 'warning'
				}).then(() => {
					this.type.listLoading = true;
          this.$http.delete("/dictionaryType/"+row.id).then(res =>{
            this.$message(res.data.msg);
            this.type.currentPage = 1;
            this.getDirTypes();
            this.type.listLoading = false;
          }).catch(res =>{
            this.$message({
              message: '系统错误，稍后尝试',
              type: 'error'
            });
            this.type.currentPage = 1;
            this.getDirTypes();
            this.type.listLoading = false;
          })
				}).catch(() => {
				});
			},
			//显示编辑界面
			handleEdit1: function (index, row) {
        this.title='编辑';
        this.type.saveForm = Object.assign({}, row);
				this.type.saveFormVisible = true;
			},
			//显示新增界面
			handleAdd1: function () {
        this.title='新增';
        this.type.saveForm = {
          id: null,
          name: '',
          sn: ''
        };
				this.type.saveFormVisible = true;
			},
			// 提叫添加或修改
      saveSubmit1: function () {
        this.$refs.saveForm1.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              let para = Object.assign({}, this.type.saveForm);
              this.$http.put("/dictionaryType/", para).then(res => {
                this.$message(res.data.msg);
                this.type.saveFormVisible = false;
                this.getDirTypes();
              }).catch(res => {
                this.$message({
                  message: '系统错误，稍后尝试',
                  type: 'error'
                });
                this.type.saveFormVisible = false;
                this.getDirTypes();
              });
            });
          }
        });
      },
      // 批量选择时参数
			selsChange1: function (sels) {
				this.type.sels = sels;
			},
			//批量删除
			batchRemove1: function () {
				var ids = this.type.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '温馨提示', {
					type: 'warning'
				}).then(() => {
					this.type.listLoading = true;
          this.$http.patch("/dictionaryType/", ids).then(res =>{
            this.$message(res.data.msg);
            this.type.currentPage = 1;
            this.type.listLoading = false;
            this.getDirTypes();
          }).catch(res =>{
            this.$message({
              message: '系统错误，稍后尝试',
              type: 'error'
            });
            this.type.currentPage = 1;
            this.type.listLoading = false;
            this.getDirTypes();
          })
				}).catch(() => {
        });
			},
      openDrawer(row, column, event){
        this.$emit('handleDrawerOpen',row.id);
      }

		},

		mounted() {
			this.getDirTypes();
		}
	}
</script>

<style scoped>

</style>